<template>
  <div id="ConsoleNet" style="width: 100%; height: 100%; background: #ffffff; text-align: center">
    <div ref="ConsoleNet" :style="{ width:'100%',height:'100%' }"></div>
  </div>
</template>

<script>
import moment from 'moment/moment'
import * as echarts from "echarts";

export default {
  name: 'ConsoleNet',
  data() {
    return {
      chart: null,
      option: {
        title: {
          show: true,
          text: '网络',
          left: 'center',
          top: 20,
        },
        grid: {
          show: true,
          right: '30px',
          containLabel: true,
        },
        xAxis: {
          type: 'time',
          // 如果需要，可以设置 min 为你的数据中的最小时间戳
          // min: 'dataMin', // 或者使用具体的时间戳
          boundaryGap: false, // 尝试移除 boundaryGap 或者设置为 false
          axisLabel: {
            formatter: (v) => {
              return moment(v).format('HH:mm:ss')
            },
            showMaxLabel: true,
            interval: 'auto',
            rotate: 45
          },
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 5,
          splitNumber: 6,
          position: 'left',
          silent: true,
        },
        tooltip: {
          trigger: 'axis',
          formatter: (data) => {
            return (
                data[1].marker +
                '下载：' +
                parseFloat(data[1].data[1]).toFixed(2) +
                'Mbps' +
                '</br> ' +
                data[0].marker +
                ' 上传：' +
                parseFloat(data[0].data[1]).toFixed(2) +
                'Mbps'
            )
          },
        },
        legend: {
          left: 'center',
          bottom: '15px',
        },
        series: [
          {
            name: '下载',
            type: 'line',
            stack: 'Total',
            data: []
          },
          {
            name: '上传',
            type: 'line',
            stack: 'Total',
            data: []
          },
        ],
      },
    }
  },
  mounted() {
    this.initChart();
    this.$nextTick((_) => {
      setTimeout(() => {
        this.chart.resize();
      }, 100)
    })
  },
  unmounted() {
  },
  methods: {
    setData: function (data, total) {
      let data0 = [];
      let data1 = [];
      data.map((item) => {
        data0.push(item.in);
        data1.push(item.in);
      });
      this.option.series[0].data = data0;
      this.option.series[1].data = data1;
      this.option.yAxis.max = total === 0 ? 5 : total;
      this.chart.setOption(this.option);
    },
    initChart() {
      this.chart = markRaw(echarts.init(this.$refs.ConsoleNet));
    }
  },
}
</script>
